package ShuZuDeChuangJianHeShiYong;

import java.sql.Array;
import java.util.Arrays;

public class FangFa2 {
    //数组变大二倍
    public static String printarray(int []array){
        String ret="{";
        for (int i=0;i<array.length;i++) {
            if (i<array.length-1) {
                ret=ret+array[i]+",";
            }
            else {
                ret=ret+array[i]+"}";
            }
        }
        return ret;
    }
    public static int [] extension(int []array){
        for (int i = 0; i <array.length ; i++) {
            array[i]=array[i]*2;
        }
        return array;
    }


    //奇数位于偶数之前
    public static String array11(int []array1){
        int count=0;
        int b=0;
        int[] newarray = new int[array1.length];
        for(int i=0;i<array1.length;i++)
        {
            if(array1[i]%2==1)
                count++;
        }

        for(int i=0;i<array1.length;i++)
        {
            if(array1[i]%2==0)
            {
                newarray[count]=array1[i];
                count++;
            }
            else {
                newarray[b]=array1[i];
                b++;
            }
        }
        return Arrays.toString(newarray);
    }



    //二分查找
    public static int find(int [] array,int x)
    {
      int left=0;
      int right=array.length-1;
      int mid=(left+right)>>1;
      if(x==array[mid]){
          return mid;
      } else if (x<array[mid]) {
          right=mid-1;
          mid=(left+right)>>1;
      }
      else {
          left=mid+1;
          mid=(left+right)>>1;
      }
      if(left>right)
      {
          System.out.println("没有找到");
      }
      return mid;
    }


    //冒泡排序（升序）
    public static String MyIntSort(int[] array){
        int i=0;
        int j=0;
        int tmp=0;
        for(i=1;i<array.length;i++){
            for(j=i;j<array.length;j++)
            {
                if(array[j-1]>array[j]){
                    tmp=array[j];
                    array[j]=array[j-1];
                    array[j-1]=tmp;
                }
            }
        }
        return Arrays.toString(array);
    }



    //求俩数之和
    public static String addarray(int[] array,int x)
    {
        int i=0;
        int j=0;
        for(i=0;i<array.length;i++)
        {
            for(j=i+1;j<array.length;j++)
            {
                if(array[i]+array[j]==x)
                {
                    return "["+i+","+j+"]";
                }
            }
        }
        return "没有找到能能加起来等于"+"x"+"的值";
    }


    //单身狗
    public static int singDog(int[] array)
    {
        int i=0;
        int tmp=0;
        for(i=0;i<array.length-1;i++)
        {
            array[i]=array[i]+array[i+1];
        }
        return array[i];
    }


    //出现次数多的元素
    public static String ChuXian(int[] array)
    {
        int i=0;
        int j=0;
        int count=0;
        int cishu=array.length/2;
        for(i=0;i<array.length;i++)
        {
            for(j=i;j<array.length;j++)
            {
                if(array[i]==array[j])
                {
                    count++;
                    if(count>cishu)
                    {
                        return "出现次数较多的元素是"+array[i];
                    }
                }
            }

        }
        return "没有出现次数较多的数组";
    }


    //判断连续三个数都为奇数
    public static boolean PanDuan(int a){
        if(a==1)
        {
            return true;
        }
        for(int i=2;i<a;i++) {
            if (a % i == 0) {
                return false;
            }
        }
        return true;
    }


    public static String panDuanLianXuChuXianCiShu(int[] array8)
    {
        for(int i=0;i<array8.length-2;i++)
        {
            if(PanDuan(array8[i])==true&&
               PanDuan(array8[i+1])==true &&
               PanDuan(array8[i+2])==true)
            {
                return "连续三个元素是"+array8[i]+","+array8[i+1]+","+array8[i+2];
            }
        }
        return "没有出现三个连续的元素是奇数";
    }
}
